/**
 * Created by Administrator on 2017/5/1.
 */
!(function($){
    var search = {
        init:function(){
            this.filter();
            this.sort();
            this.getCats();
            this.searchRun();
        },
        getCats:function(){
            var self = this;
            $.ajax({
                url: API_URL + GET_CATEGORYS_URL,
                type: 'get',
                dataType: 'jsonp',
                data:{type:0},
                success: function (data) {
                    //分类拼接
                    var datas= data.result;
                    var json = {};
                    var arrs = [];
                    for(var i = 0;i<datas.length;i++){
                        json = {title:datas[i].name,value:datas[i].id};
                        arrs.push(json);
                    }
                    arrs.push({title:'全品类',value:'0'});
                    $("#getCats").select({
                        title: "选择分类",
                        items: arrs,
                        onChange: function(d) {
                            var keyword = $('#weui_search_input').val();
                            var from = $('#type_from').val();
                            var type = $('#type_type').val();
                            var orderBy = $('#type_orderby').val();
                            $('#getCats').find('.txt').html(d.titles);
                            self.searchFun(keyword,type,from,orderBy,d.values);
                            $('#type_categoryId').val(d.values);
                        }
                    });
                }
            });
        },
        searchFun:function(keyword,type,from,orderBy,categoryId){
            $.ajax({
                url:API_URL+GET_PRODUCTINFO_URL,
                type:'get',
                dataType:'jsonp',
                data:{keyword:keyword,type:type,from:from,orderBy:orderBy,categoryId:categoryId,page:0,pageSize:10},
                success:function(data){
                    if(data){
                        //信息列表
                        var tpl = template('search_tpl',data);
                        $('.shop-list').html(tpl);
                        new Swiper('.shop-list .swiper-container', {
                            slidesPerView: 3,
                            spaceBetween: 10
                        });
                    }

                }
            });

        },
        searchRun:function(){
            var self = this;
            $('#btn-search').on("click",function(){
                var keyword = $('#weui_search_input').val();
                if(keyword==''){
                    $.alert('请输入搜索词');
                    return;
                }
                var from = $('#type_from').val();
                var type = $('#type_type').val();
                var categoryId = $('#type_categoryId').val();
                var orderBy = $('#type_orderby').val();
                self.searchFun(keyword,type,from,orderBy,categoryId);
            });
        },
        filter:function(){
            var self = this;
            $("#getResource").select({
                title: "选择来源",
                items: [
                    {
                        title:"不限",
                        value:"0"
                    },
                    {
                        title: "商家",
                        value: "1"
                    },
                    {
                        title: "个人",
                        value: "2"
                    }
                ],
                onChange: function(d) {
                    var type = $('#type_type').val();
                    var keyword = $('#weui_search_input').val();
                    var categoryId = $('#type_categoryId').val();
                    self.searchFun(keyword,type,d.values,0,categoryId);
                    $('#type_from').val(d.values);
                }
            });
        },
        sort:function(){
            var self = this;
            var i = 0;
            $('#slastest').on('click',function(){
                var type = $('#type_type').val();
                var keyword = $('#weui_search_input').val();
                var categoryId = $('#type_categoryId').val();
                var from = $('#type_from').val();
                if(keyword==''){
                    $.alert('请输入关键词');
                    return;
                }
                i++;
                if(i%2==0){
                    self.searchFun(keyword,type,from,0,categoryId);
                }
            });
            $('#sprice_sort').on('click',function(){
                var type = $('#type_type').val();
                var keyword = $('#weui_search_input').val();
                var categoryId = $('#type_categoryId').val();
                var from = $('#type_from').val();
                if(keyword==''){
                    $.alert('请输入关键词');
                    return;
                }
                i++;
                if(i%2==0){
                    self.searchFun(keyword,type,from,1,categoryId);
                    $('.icon-sortsvg').find('use').attr('xlink:href','#icon-sort-small1');

                }else{
                    self.searchFun(keyword,type,from,2,categoryId);
                    $('.icon-sortsvg').find('use').attr('xlink:href','#icon-sort-small2');

                }
            })
        }
    };
    $(function(){
        search.init();
        $('#weui_search_input').trigger('click');
    })
})(Zepto);